package com.xunwulian.cmpp.client.handler;

import com.xunwulian.cmpp.pdu.CmppPDU;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO: Document me !
 *
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 */
public class CmppResponseEncoder implements ProtocolEncoder {

    private static final Logger logger = LoggerFactory.getLogger(CmppResponseEncoder.class);

    public void encode(IoSession session, Object message,
                       ProtocolEncoderOutput out) throws Exception {
        CmppPDU pdu = (CmppPDU) message;
        logger.info("send pdu");
        byte[] bytes = pdu.getData().getBuffer();
        IoBuffer buf = IoBuffer.allocate(bytes.length, false);
        buf.setAutoExpand(true);
        // buf.putInt(bytes.length);
        buf.put(bytes);
        buf.flip();
        logger.info("out.write(buf)");
            out.write(buf);
    }

    public void dispose(IoSession session) throws Exception {
    }
}